<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
        <el-button
        type="success"
        size="mini"
        @click="handleImport"
      >导入学生信息</el-button>
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="importStudentInfoList" @selection-change="handleSelectionChange">
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="工作单位" align="center" prop="workplace" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column v-if="qualifiedType == '特种作业人员'" label="操作项目" align="center" prop="operationType"/>
      
      <el-table-column v-if="qualifiedType == '主要负责人' || qualifiedType == '安全生产管理人员'" label="考试地点" align="center" prop="examPlace">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.exam_place" :value="scope.row.examPlace"/>
        </template>
      </el-table-column>
      
      <el-table-column label="个人申请表(附件)" align="center" prop="urlPersonApplication" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlPersonApplication" :width="45" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="健康承诺书(附件)" align="center" prop="urlHealthCertificate" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlHealthCertificate" :width="45" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="学历证明(附件)" align="center" prop="urlDegreeCertificate" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlDegreeCertificate" :width="45" :height="50"/>
        </template>
      </el-table-column>
 
      <el-table-column label="身份证复印件(附件)" align="center" prop="urlIdCopy" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlIdCopy" :width="45" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            style="width: 40px;"
            @click="check(scope.row)"
          >查看</el-button>

          <el-button
          style="margin-top: 10px;"
          v-if="scope.row.qualifiedType == '特种作业人员'"
            size="mini"
            type="success"
            @click="handleUpdate(scope.row)"
          >添加实操结论</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="checkOpen" width="1200px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="100px">
        
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        
        <el-form-item label="工作单位" prop="workplace">
          <el-input v-model="form.workplace" placeholder="请输入工作单位" />
        </el-form-item>

        <el-form-item label="理论成绩" prop="examScore">
          <el-input v-model="form.examScore" placeholder="请导入理论成绩" />
        </el-form-item>

        <el-form-item  v-if="form.qualifiedType == '特种作业人员'" label="实操结论" prop="operatingScore">
          <el-input v-model="form.operatingScore" placeholder="请输入实操结论" />
        </el-form-item>

        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        
        <el-form-item v-if="form.qualifiedType == '特种作业人员'" label="操作项目" prop="operationType">
          <el-input v-model="form.operationType" placeholder="请输入操作项目" />
        </el-form-item>

        <el-form-item v-if="form.qualifiedType == '主要负责人' || form.qualifiedType == '安全生产管理人员'" label="安全资格类型" prop="workKind">
          <el-input v-model="form.workKind" placeholder="请输入安全资格类型" />
        </el-form-item>

        <el-form-item label="培训是否合格" prop="trainIsPass">
          <el-select v-model="form.trainIsPass" placeholder="培训是否合格">
            <el-option
              v-for="dict in dict.type.train_is_pass"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考试地点" prop="examPlace">
          <el-select v-model="form.examPlace" placeholder="考试地点">
            <el-option
              v-for="dict in dict.type.exam_place"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请材料项数" prop="applyDataNumber">
          <el-input v-model="form.applyDataNumber" placeholder="申请材料项数" />
        </el-form-item>
        <el-form-item label="考生是否委托培训机构代办" prop="isAgent">
          <el-select v-model="form.isAgent" placeholder="考生是否委托培训机构代办">
            <el-option
              v-for="dict in dict.type.is_agent"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="个人申请表(附件)" prop="urlPersonApplication">
          <image-upload v-model="form.urlPersonApplication"/>
        </el-form-item>
        <el-form-item label="健康承诺书(附件)" prop="urlHealthCertificate">
          <image-upload v-model="form.urlHealthCertificate"/>
        </el-form-item>
        <el-form-item label="学历证明(附件)" prop="urlDegreeCertificate">
          <image-upload v-model="form.urlDegreeCertificate"/>
        </el-form-item>
        <el-form-item label="身份证复印件(附件)" prop="urlIdCopy">
          <image-upload v-model="form.urlIdCopy"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCheck">返 回</el-button>
      </div>
    </el-dialog>


    <!-- 添加考试结论 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form  ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="实操结论" prop="operatingScore">
          <el-input type="textarea" v-model="form.operatingScore" placeholder="请填入实操结论" />
        </el-form-item>
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
      <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
        <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
        >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:20px; color: green;" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
import { listImportStudentInfo, getImportStudentInfo, delImportStudentInfo, addImportStudentInfo, updateImportStudentInfo } from "@/api/student/importStudentInfo";
import { getUrlParam } from '../../../api/utils';
import { importTemplate } from "@/api/system/user";
import { getToken } from "@/utils/auth";

export default {
  name: "ImportStudentInfo",
  dicts: ["operation_type", "exam_place", "is_agent", "train_is_pass", "is_pay", 'qualified_type'],
  data() {
    return {
      // 查看信息
      checkOpen: false,
      qualifiedType: "",
      // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/student/importStudentInfo/importData"
        },
      // 考试申请表id
      examApplicationId: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 导入学生信息表格数据
      importStudentInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.examApplicationId = getUrlParam("examApplicationId");
    this.getList();
  },
  watch: {
   $route(to, from) {
       window.location.reload(); //监测到路由发生跳转时刷新一次页面
   },
},
  methods: {
    checkInfo(info) {
      console.log(info)
    },
    /** 查询导入学生信息列表 */
    getList() {
      this.loading = true;
      listImportStudentInfo(this.examApplicationId).then(response => {
        this.importStudentInfoList = response.rows;
        this.qualifiedType = this.importStudentInfoList[0].qualifiedType;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNumber: null,
        name: null,
        idCard: null,
        workplace: null,
        phone: null,
        urlPersonApplication: null,
        urlHealthCertificate: null,
        urlDegreeCertificate: null,
        urlIdCopy: null,
        createId: null,
        updateId: null,
        createTime: null,
        updateTime: null,
        isDeleted: null,
        extend: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加导入学生信息";
    },
    // 查看学生信息
    check(row) {
      this.reset();
      const id = row.id || this.ids
      getImportStudentInfo(id).then(response => {
        this.form = response.data;
        this.checkOpen = true;
        this.title = "查看学生信息";
      });
    },
    // 取消查看学生信息
    submitCheck() {
      this.checkOpen = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getImportStudentInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改导入学生信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateImportStudentInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addImportStudentInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除导入学生信息编号为"' + ids + '"的数据项？').then(function() {
        return delImportStudentInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('student/importStudentInfo/export', {
        ...this.queryParams
      }, `importStudentInfo_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "学生基本信息导入";
      this.upload.open = true;
    },
    //** 下载模板操作 */
    importTemplate() {
      this.download('/student/importStudentInfo/importTemplate', {
      }, `student_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }

  }
};
</script>
